<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,util.*,dal.*,model.*,bll.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
	int idQuotation = MiscUtil.getIntUrlParam(request, "idQuotation");
	int idParent = MiscUtil.getIntUrlParam(request, "idParent");

	if (idQuotation == Integer.MIN_VALUE) {
		response.sendRedirect("quotationNew.jsp");
	}
	
	
	QuotationModel quotation = CacheUtil.getQuotation(idQuotation);
	
	String quotationName = quotation.getName();
	String quotationUrl = "quotation.jsp?id=" + idQuotation;

	String projectName = CacheUtil.getProjectName(quotation.getIdProject());
	String projectUrl = "project.jsp?id=" + quotation.getIdProject();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create New Quotation Item</title>
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/inputForm.css" />
<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-datepicker.css" />
<link rel="stylesheet" href="css/jquery-dialog.css" />

<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript">
	$(document).ready(pageLoaded);
	function pageLoaded()
	{
		turnOnNumbericInput();
		initTooltip();
	}
	
	function toggleIsGroupFields()
	{
		toggleControls(['tblUnit', 'tblEstimateQuantity', 'tblEstimateUnitCost', 'tblEstimateTotalAmount','tblInQuotation','tblRealQuantity','tblRealUnitCost','tblRealTotalAmount']);
		
		if(isInQuotation() == true)
			toggleInQuotationFields();
		if(isIsGroup()== true)
		{
			$("#tdContainer").css("padding-left","400px");
			$("#tdContainer").css("padding-right","400px");
			$(".btnSubmit").css("margin-left","-40px");
		}
		else
		{
			$("#tdContainer").css("padding-left","250px");
			$("#tdContainer").css("padding-right","250px");
			$(".btnSubmit").css("margin-left","40px");			
		}
		
	}
	function toggleInQuotationFields()
	{
		toggleControls(['tblQuotationQuantity','tblQuotationUnitCost','tblQuotationTotalAmount']);
	}
	function isInQuotation()
	{
		if(document.getElementById("in_quotation").checked==true)
			return true;
		else
			return false;
	}
	function isIsGroup()
	{
		if(document.getElementById("is_group").checked==true)
			return true;
		else
			return false;
	}
	var requiredInputs = [['display_index','display_index_required'],['name','name_required']];
	var requiredIsGroupInputs = [['estimate_quantity','estimate_quantity_required'],['estimate_unit_cost','estimate_unit_cost_required'],['estimate_total_amount','estimate_total_amount_required'],['internal_quantity','internal_quantity_required'],['internal_unit_cost','internal_unit_cost_required'],['internal_total_amount','internal_total_amount_required']];
	var requiredInQuotationInputs = [['quotation_quantity','quotation_quantity_required'],['quotation_unit_cost','quotation_unit_cost_required'],['quotation_total_amount','quotation_total_amount_required']];
	var intInputs = [];
	var doubleInputs = [];
	var dateInputs = [];
	var timeInputs = [];


	function checkInputs() {
		var valid = true;

		if (checkRequiredInputs(requiredInputs) == false) {
			valid = false;
		}
		
		if (checkRequiredIsGroupInputs(requiredIsGroupInputs,'is_group') == false) {
			valid = false;
		}
		
		if (isIsGroup() == false && checkRequiredInQuotationInputs(requiredInQuotationInputs,'in_quotation') == false) {
			valid = false;
		}
		
// 		if (checkIntInputs(intInputs) == false) {
// 			valid = false;
// 		}
		
// 		if (checkDoubleInputs(doubleInputs) == false) {
// 			valid = false;
// 		}
		
// 		if (checkDateInputs(dateInputs) == false) {
// 			valid = false;
// 		}
// 		if (checkTimeInputs(timeInputs) == false) {
// 			valid = false;
// 		}

		// SPECIFIC VALIDATE CODE HERE		

		return valid;
	}
	
	
</script>
</head>
<body>
<div class="divContainer">
		<%@ include file="menu.jsp"%>
				<form id="inputForm" action="quotationItem?mode=insert&id_quotation=<%=idQuotation%>" method="post" onSubmit="return checkInputs();" onreset="return confirm('Do you really want to reset the form?');">
					<table id="tblInputFields" class="tblInputSmall" >
						<tr><td colspan="1">
							<b>Project: <a id="link_project" name="link_project" href="<%=projectUrl %>"><%=projectName %></a></b>
							<br />&gt;
							<b>Quotation: <a id="link_checklist" name="link_checklist" href="<%=quotationUrl %>"><%=quotationName %></a></b>
						</td></tr>
						
						<tr >
							<td class="inputHeader">CREATE NEW QUOTATION ITEM</td>
						</tr>
						<tr>
							<td id="tdContainer" style="padding-left: 250px; padding-right: 250px;">
						<table cellspacing="0px" >
						<tr>
						<td >
							<table id="tblParent" >
								<tr>
									<td class="tdFieldName">Parent</td>
									<td class="tdFieldInput"><select id="id_parent" name="id_parent" class="hyjack">
										<%=HtmlUtil.<QuotationItemModel> genModelSelect(CacheUtil.getIsGroupQuotationItemList(idQuotation), null, idParent, false, true)%>
										</select>
									</td>
									<td class="tdErrMsg">
									</td>
								</tr>
							</table>
						</td>
 						<td rowspan="10" style="width: 30px;"></td>
						<td>
							<table id="tblEstimateQuantity" >
								<tr>
									<td class="tdFieldNameSmall">Estimate Quantity</td>
									<td class="tdFieldInputSmall"><input class="inputInt" type="text" id="estimate_quantity" name="estimate_quantity" maxlength="10" title="Quantity in estimate. <br/>Note: integer only." tabindex="8"  onblur="validateRequiredInput('estimate_quantity');"/>
									</td>
									<td class="tdErrMsg">
										<span id="estimate_quantity_required" class="errMsg" style="display: none;" title="Esitimate quantity is a required field.">(!)</span> 
										<span id="estimate_quantity_invalid" class="errMsg" style="display: none;" title="You've entered an invalid">(!)</span> 
									</td>	
								</tr>
							</table>
						</td>						
						</tr>
						
						<tr>
						<td>
							<table id="tblDisplayIndex" >
								<tr>
									<td class="tdFieldName">Display Index</td>
									<td class="tdFieldInput"><input class="inputText" title="This looks like bullet and numbering in Word." type="text" id="display_index" name="display_index" maxlength="10" tabindex="1"  onblur="validateRequiredInput('display_index');"/>
									</td>
									<td class="tdErrMsg">
										<span id="display_index_required" class="errMsg" style="display: none;" title="Display index is a required field. Value must be entered to it.">(!)</span> 
										<span id="display_index_invalid" class="errMsg" style="display: none;" title="You've entered an invalid display index."></span> 
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblEstimateUnitCost" >
								<tr>	
									<td class="tdFieldNameSmall">Estimate Unit Cost</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" type="text" id="estimate_unit_cost" name="estimate_unit_cost" title="Unit cost in estimate. <br/>Note: numeric only." maxlength="20" tabindex="9"  onblur="validateRequiredInput('estimate_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
										<span id="estimate_unit_cost_required" class="errMsg" style="display: none;" title="Unit cost is a required field.">(!)</span> 
										<span id="estimate_unit_cost_invalid" class="errMsg" style="display: none;" title="You've entered an invalid unit cost.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						</tr>
								
						<tr>
						<td>
							<table id="tblName" >
								<tr>
									<td class="tdFieldName">Name</td>
									<td class="tdFieldInput"><input class="inputText" type="text" id="name" name="name" maxlength="255" title="Name of quotation item." tabindex="2"  onblur="validateRequiredInput('name');"/>
									</td>
									<td class="tdErrMsg">
										<span id="name_required" class="errMsg" style="display: none;" title="Name is a required field.">(!)</span> 
										<span id="name_invalid" class="errMsg" style="display: none;" title="You've enter an invalid name.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblEstimateTotalAmount" >
								<tr>
									<td class="tdFieldNameSmall">Estimate Total Amount</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" type="text" id="estimate_total_amount" title="Total amount in estimate. Note: numeric only." name="estimate_total_amount" maxlength="20" tabindex="10"  onblur="validateRequiredInput('estimate_total_amount');"/>
									</td>
									<td class="tdErrMsg">
										<span id="estimate_total_amount_required" class="errMsg" style="display: none;" title="Estimate total amount is a required field.">(!)</span> 
										<span id="estimate_total_amount_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
							<table id="tblDescription" >
								<tr>
									<td class="tdFieldName">Description</td>
									<td class="tdFieldInput"><textarea id="description" name="description" rows="5" class="inputTextarea" title="Description for this quotation item." tabindex="3"></textarea>
									</td>
									<td class="tdErrMsg">
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealQuantity" >
								<tr>
									<td class="tdFieldNameSmall">Real Quantity</td>
									<td class="tdFieldInputSmall"><input class="inputInt" type="text" id="internal_quantity" name="internal_quantity" maxlength="10" title="Real quatity. <br/>Note: only integer allowed." tabindex="12"  onblur="validateRequiredInput('internal_quantity');"/>
									</td>
									<td class="tdErrMsg">
										<span id="internal_quantity_required" class="errMsg" style="display: none;" title="Real quantity is required field.">(!)</span> 
										<span id="internal_quantity_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						</tr>
					
						<tr>
						<td>
							<table id="tblRemark" >
								<tr>
									<td class="tdFieldName">Remark</td>
									<td class="tdFieldInput"><textarea id="remark" name="remark" title="Remark of this quotation item." rows="5" class="inputTextarea" tabindex="4"></textarea>
									</td>
									<td class="tdErrMsg">
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealUnitCost" >
								<tr>
									<td class="tdFieldNameSmall">Real Unit Cost</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" title="Real unit cost. <br/>Note: only numeric allowed." type="text" id="internal_unit_cost" name="internal_unit_cost" maxlength="20" tabindex="13"  onblur="validateRequiredInput('internal_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
										<span id="internal_unit_cost_required" class="errMsg" style="display: none;" title="Real unit cost is a required field.">(!)</span> 
										<span id="internal_unit_cost_invalid" class="errMsg" style="display: none;" title="You've entered an invalid unit cost.">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
					
						<tr>
						<td>
							<table id="tblIsGroup" >
								<tr>
									<td class="tdFieldName">Is group</td>
									<td class="tdFieldInput"><input class="inputCheckbox" title="Check this option to mark this quotation item is a group." type="checkbox" id="is_group" name="is_group" onchange="toggleIsGroupFields();" tabindex="5"/>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealTotalAmount" >
								<tr>
									<td class="tdFieldNameSmall">Real Total Amount</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" title="Real total amount. <br/>Note: only numeric allowed." type="text" id="internal_total_amount" name="internal_total_amount" maxlength="20" tabindex="14"  onblur="validateRequiredInput('internal_total_amount');"/>
									</td>
									<td class="tdErrMsg">
										<span id="internal_total_amount_required" class="errMsg" style="display: none;" title="Real total amount is a required field.">(!)</span> 
										<span id="internal_total_amount_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
									</td>
									</tr>
							</table>
						</td>	
						</tr>
						
						<tr>
						<td>
							<table id="tblUnit" >
								<tr>
									<td class="tdFieldName">Unit</td>
									<td class="tdFieldInput"><select id="id_unit" name="id_unit" class="hyjack" tabindex="6">
										<%=HtmlUtil.<UnitModel>genModelSelect(CacheUtil.getUnitList(), null, Integer.MIN_VALUE, false, true)%>
										</select><input class="btnAdd" type="button" id="addNewIdUnit" value="" onclick="ajaxAddItem('#id_unit', 'unit', 'name', 'Please enter your new Unit name!');" title="Click here to add new Unit." tabindex="7"/>
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblInQuotation" >
								<tr>
									<td class="tdFieldNameSmall">In Quotation</td>
									<td class="tdFieldInputSmall"><input class="inputCheckbox" type="checkbox" title="Check to mark this is in quotation." id="in_quotation" name="in_quotation" value checked onchange="toggleInQuotationFields();" tabindex="14"/> 
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
						</td>	
						<td>
							<table id="tblQuotationQuantity" >
								<tr>
									<td class="tdFieldNameSmall">Quotation Quantity</td>
									<td class="tdFieldInputSmall"><input class="inputInt" type="text" title="Quantity in quotaion. <br/>Note: only integer allowed." id="quotation_quantity" name="quotation_quantity" maxlength="10" tabindex="15"  onblur="validateRequiredInput('quotation_quantity');"/>
									</td>
									<td class="tdErrMsg">
										<span id="quotation_quantity_required" class="errMsg" style="display: none;" title="Quotation quantity is an required field.">(!)</span> 
										<span id="quotation_quantity_invalid" class="errMsg" style="display: none;" title="You've enter an invalid quotation quantity.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						
				
						<tr>
						<td>
						</td>	
						<td>
							<table id="tblQuotationUnitCost" >
								<tr>
									<td class="tdFieldNameSmall">Quotation Unit Cost</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" type="text" id="quotation_unit_cost" name="quotation_unit_cost" title="Unit cost in quotation. <br/>Note: only numeric allowed." maxlength="20" tabindex="16"  onblur="validateRequiredInput('quotation_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
									<span id="quotation_unit_cost_required" class="errMsg" style="display: none;" title="Quotation unit cost is a required field.">(!)</span> 
										<span id="quotation_unit_cost_invalid" class="errMsg" style="display: none;" title="you've entered an invalid value.">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
						</td>
						<td>
							<table id="tblQuotationTotalAmount" >
								<tr>
									<td class="tdFieldNameSmall">Quotation Total Amount</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" type="text" id="quotation_total_amount" name="quotation_total_amount" title="Total amount in quotation. Note: only numeric allowed." maxlength="20" tabindex="17"  onblur="validateRequiredInput('quotation_total_amount');"/>
									</td>
									<td class="tdErrMsg">
										<span id="quotation_total_amount_required" class="errMsg" style="display: none;" title="Quotation total amount is a required field.">(!)</span> 
										<span id="quotation_total_amount_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
									</td>
								</tr>
							</table>
						</td>	
						</tr>
	
						<tr class="trInputBottom">
							<td class="trInputBottom" colspan="3">
								<table class="tblSubmit" align="center" >
									<tr class="trSubmit">
										<td class="tdSubmit" style="text-align: center;">
											<!-- To modify the position of btnSubmt, plz take a look at toggleIsGroupFields() -->
											<input class="btnSubmit" style="margin-left: 40px;" type="submit" value="Submit" title="Click to save." tabindex="18"/>
											<input class="btnReset leftSeparation" type="reset" value="Reset" title="Click to reset all fields." tabindex="19"/>
										</td>
									</tr>
								</table></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr class="trSeparator">
				<td></td>
			</tr>
		</table>
	</form>
	<%
		//String str =  "quotationItemList.jsp?embedded=true&idQuotation=" + idQuotation;
		String str = "quotation.jsp?embedded=true&id=" + idQuotation;
	%>
	<jsp:include page='<%=str%>'></jsp:include>
	</div>
</body>
</html>